using System;
using System.Collections;
using PHSRAG.Insight21.Common.Pages;
using SSRS;

namespace PHSRAG.Insight21.Reports
{
    public partial class sqlReportHost : BasePage
    {
        private sqlReport _objReport;
        private string _pageUrl = string.Empty;
        private ReportFactory _reportFactory;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    string srptName = Request["rpt"]; //name of the report to serve up
                    string format = Request["fmt"]; //format of the report - PDF or Excel
                    _pageUrl = Request["pageUrl"]; //format of the report - PDF or Excel

                    if (!string.IsNullOrEmpty(srptName))
                    {
                        //string reportName = srptName;
                        _reportFactory = new ReportFactory();
                        _objReport = (sqlReport) _reportFactory.getReport(_pageUrl, srptName, format);
                        _objReport.ReportName = srptName;
                        _objReport.rptFormat = format;

                        // do custom Excel report with formula
                        if (format.ToLower() == "excel" && _objReport.doCustomExcelReport)
                        {
                            format = _objReport.CustomExcelFormat;
                        }

                        if (_objReport.doHashtable == false)
                        {
                            ParameterValue[] rptPars = GetInputParameters(ref srptName);
                            SQLRequest.FetchAndDisplayReport(format, srptName, rptPars);
                        }
                        else
                        {
                            Hashtable ht = GetInputHashTable(ref srptName);
                            SQLRequest.FetchAndDisplayReport(format, srptName, ht);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Response.Write("There was a problem processing your request. Please contact helpdesk. <br>" + ex.Message);
                }
            }
        }

        # region InputParameters

        private ParameterValue[] GetInputParameters(ref string srptName)
        {
            return _objReport.setParameters(ref srptName);
        }

        private Hashtable GetInputHashTable(ref string srptName)
        {
            return _objReport.setParameterHTable(ref srptName);
        }

        #endregion
    }
}